<template>
  <el-dialog width="1200px" :title="$t('m.otherstore.storagecode')" :visible.sync="popupVisible" :before-close="close">
    <!-- storageBasicContent start -->
    <storageBasicContent ref="content" v-bind="$attrs"></storageBasicContent>
    <!-- storageBasicContent end -->
    <el-row>
      <el-col :span="24" align="right">
        <el-button @click="close">{{$t('m.close')}}</el-button>
        <el-button type="primary" @click="storageSave" :loading="btnLoading">{{$t('m.confirm')}}</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import storageBasicContent from './storage-basic-content'
export default {
  name:'storage-add-popup',
  props:['popupVisible'],
  data(){
    return {
      btnLoading:false // 控制按钮loading，防止重复提交
    }
  },
  mounted(){

  },
  components:{storageBasicContent},
  methods:{
    close(){
      this.$emit('close')
    },
    // 保存其他入库单
    storageSave(){
      this.$refs['content'].$refs['form'].validate((valid) => {
        this.$refs['content'].$refs['table'].validate((__valid)=>{
          let _this = this.$refs['content']
          if (valid&&__valid){
            let warehouseid = this.$store.state.basicInfo.warehouseInfo.id
            let {consignorname} = _this.consignorOptions.find((item)=>{
              return item.consignorid === _this.formDetail.consignorid
            })
            let {suppliername} = _this.supplierOptions.find((item)=>{
              return item.supplierid === _this.formDetail.supplierid
            })
            let {typevalue:currency} = _this.currencyOptions.find((item)=>{
              return item.typevalueid === _this.formDetail.currencyid
            })
            let totalamount = _this.totalPrice || 0 // 合计金额
            let totalnumber = _this.totalAmount || 0 // 合计数量
            let data = Object.assign(_this.formDetail,{consignorname},{suppliername},{currency},{totalamount},{totalnumber},{warehouseid})

            for(let i=0;i<_this.tableDetail.tableData.length;i++){
              let quantity = 0 // 计算单项总数量
              for(let j=0;j<_this.tableDetail.tableData[i].storageQuantitys.length;j++){
                quantity += parseInt(_this.tableDetail.tableData[i].storageQuantitys[j].quantity)
              }
              _this.tableDetail.tableData[i].quantity = quantity
            }
            data.otherStorageDetails = this.cloneObject(_this.tableDetail.tableData) // 统一数据格式（新增接口的data格式）
            for(let i=0;i<data.otherStorageDetails.length;i++){
              for(let j=0;j<data.otherStorageDetails[i].batchattr.length;j++){
                delete data.otherStorageDetails[i].batchattr[j].validateFn
              }
              data.otherStorageDetails[i].batchattr = JSON.stringify(data.otherStorageDetails[i].batchattr)
            }

            // 区分 【新增 || 编辑】 功能
            let url = !this.$attrs.detail?`/otherstorage/add`:`/otherstorage/${this.$attrs.detail.storageid}`
            let method = !this.$attrs.detail?'post':'put'

            // 接口数据格式不一致，只能重构编辑接口的data格式
            if(this.$attrs.detail){
              let editData = {}
              let {otherStorageDetails,...rest} = data
              editData.otherStorageDetailInputList = otherStorageDetails
              editData.otherStorageInputDto = rest
              data = editData
            }
            this.btnLoading = true
            this.$axios({method,url,data}).then(({message})=> {
              this.btnLoading = false
              this.callbackMessage(message,'success')
              this.$emit('successCallback')
              this.close()
            }).catch(()=>{
              this.btnLoading = false
            })
          }
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>

</style>